package demo1;

class Shape{
    public void draw(){
        System.out.println("画图形");
    }
}


class Rect extends Shape{
    public void draw(){
        System.out.println("画矩形");
    }
}

class Cycle extends Shape{
    @Override
    public void draw(){
        System.out.println("画圆");
    }
}

class Triangle extends Shape{
    public void draw(){
        System.out.println("画三角形");
    }
}

class Flower extends Shape{
    public void draw(){
        System.out.println("花！");
    }
}
public class Test {
    public static void main1(String[] args) {
        Shape shape = new Rect();
        shape.draw(); //这里画矩形

        Shape shape2 = new Cycle();
        shape2.draw(); //这里画圆

        Shape shape3 = new Triangle();
        shape3.draw(); //画三角形
    }
    //这样调太麻烦了，我们可以利用多态简化

    public static void drawMap(Shape shape){
        shape.draw();
    }
    //在drawMap方法中，Shape shape引用的子类对象不一样，调用方法表现出来的行为不一样，这种思想就叫做多态
    public static void main(String[] args) {
        drawMap(new Rect());   //这里用匿名对象，省略了创建对象这一步
        drawMap(new Cycle());
        drawMap(new Triangle());
        drawMap(new Flower());
    }

    //如果有个题目，给你一个字符串数组，让你根据里面的值花对应的图形
    //如果不用多态，只能用大量的if else来搞，如下
    public static void drawShapes1(){
        String[] strings = {"Cycle","Rect","Cycle","Rect","Flower"};
        for(String x  :strings){
            if(x.equals("Cycle")){
                Cycle cycle = new Cycle();
                cycle.draw();
            }
            else if(x.equals("Rect")){
                Rect rect = new Rect();
                rect.draw();
            }
            else{
                Flower flower = new Flower();
                flower.draw();
            }
        }
    }

    //所以我们来使用多态
    public static void drawShapes(){
        Shape[] shapes = {new Cycle(),new Rect(),new Cycle(),new Rect(),new Flower()};
        //这是个数组，里面都是Shape类型的，这里发生了向上转型关系
        for(Shape s : shapes){
            s.draw();
        }
    }
}
